package com.nbgroup.service;

import com.nbgroup.entity.Menu;
import com.nbgroup.entity.MenuRole;
import com.nbgroup.utils.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 18545 on 2019/7/10.
 */
@Service
public class MenuService {
    @Autowired
    private QueryUtil queryUtil;
    @Autowired
    RoleService roleService;
    public   List<Menu> getMenu(){
        String sql = "SELECT * FROM menu ORDER BY LEVEL";
        List<Menu> menu = queryUtil.query(sql, Menu.class);
        return menu;
    }
    public   List<Menu> getMenuByRoleId(String uresId){
        Long roleId = roleService.findRoleByUserId(uresId).getRoleId();
        String sql = "SELECT r.* FROM    menu_role r  WHERE  r.role_id ='"+roleId+"'";
        List<MenuRole> menu = queryUtil.query(sql, MenuRole.class);
        String menuId = "0,";
        if(menu.size()>0){
            menuId = menu.get(0).getMenuId();
        }
        String sq2 = "SELECT m.* FROM    menu m WHERE m.id in ("+        menuId.substring(0,menuId.length()-1)+" )ORDER BY m.LEVEL";
        List<Menu> menus = queryUtil.query(sq2, Menu.class);
        return menus;
    }
}
